<html>
<head>
	<meta charset='utf-8' />
	<title>小区信息助手后台</title>
	<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
	<style>
		@media screen and (max-width:450px){
			#content,.nav_div{width:98%;margin:0 auto}
			/*页面宽度900以下时 .abc的宽度为450*/
		}
		@media screen and (min-width:450px){
			#content,.nav_div{width:450px;margin:0 auto}
			/*页面宽度900以下时 .abc的宽度为450*/
		}
	</style>
</head>
<body>
<div id='content' style='position:relative;border:0px solid'>
	<h1 style='text-align:center'>管理成员</h1>
	
	<div>
		<input type="text" id='search_inp'>
		<button id='search_btn'>搜索</button>
	</div>

	<div id='user_list_area'>
		<div style='display:flex;margin:20px 0'>
			<div style='flex:1'>x_jilin_tonghua_001</div>
			<div style='font-size:12px'>
				<span style='margin:0 20px'>正常</span>
				<button>更改状态</button>
				<a href="after_con_admin_perm.html"><button>权限</button></a>
				<button>删除</button>
			</div>
		</div>
	</div>

	<div id='user_add_area' style='display:none'>
		<input type="text" id='user_add_inp'/>
		<button id='user_add_btn'>添加</button>
		<button id='user_back_btn'>取消</button>
	</div>
	<button id='user_add_area_btn'>添加成员</button>

	<div id='fpage' style='margin:50px 0;text-align:center'></div>
</div>



</body>
</html>
<script src='../public/jquery.js'></script>
<script src='../public/fun.js'></script>
<script>
var pnow = 1;
if(sessionStorage.after_con_admin_pnow){
	pnow = parseInt(sessionStorage.after_con_admin_pnow);
}
var psize = 3;
var max_page;
$(function(){

	//底部导航与显示页面
	$.ajax({
		url:'after_template_bottom_nav.html',
		success(res){
			$('body').append(res);
		}
	});

	//搜索
	$('#search_btn').click(function(){
		pnow = 1;
		sessionStorage.after_con_admin_pnow = 1;
		sessionStorage.after_con_admin_search = $('#search_inp').val().trim();
		get_admin_user_list();	//获取用户列表 
	});
	if(sessionStorage.after_con_admin_search){
		$('#search_inp').val(sessionStorage.after_con_admin_search);
	}


	
	get_admin_user_list();	//获取用户列表 





	//添加新用户
	$('#user_add_area_btn').click(function(){
		$('#user_add_area').show();
		$(this).hide();
	});
	$('#user_back_btn').click(function(){
		$('#user_add_area').hide();
		$('#user_add_area_btn').show();
	});
	$('#user_add_btn').click(function(){
		var user = $('#user_add_inp').val().trim();
		if(!user){
			return;
		}
		Ajax2({type:'add_admin_user',user:user},function(res){
			//C(res);
			switch(res){
				case 'err1':A('缺少参数！');break;
				case 'err2':A('用户已存在！');break;
				case 'ok':
					A('操作成功！');$('#user_add_inp').val('');
					$('#user_add_area').hide();
					$('#user_add_area_btn').show();
				break;
			}
		});
	});
});

//获取用户列表 
function get_admin_user_list(){
	Ajax2({type:'get_admin_user_list',pnow:pnow,psize:psize,search:sessionStorage.after_con_admin_search},function(res){
		//C(res);
		max_page = max_page?max_page:pnow+1;
		if(/^\[.*\]$/.test(res)){
			var user_list = JSON.parse(res);
			var user_list_str = '';
			for(var i=0;i<user_list.length;i++){
				user_list[i].stat = user_list[i].stat==1?'正常':'<span style="color:red">已封禁</span>';
				user_list_str += "<div style='display:flex;margin:20px 0'>";
					user_list_str += "<div style='flex:1'>" +user_list[i].user+ "</div>";
					user_list_str += "<div style='font-size:12px'>";
						user_list_str += "<span style='margin:0 20px'>"+user_list[i].stat+"</span>";
						user_list_str += "<button onclick=change_admin_user_stat("+user_list[i].id+")>更改状态</button>　";
						user_list_str += "<button onclick=jump_perm("+user_list[i].id+",'"+user_list[i].user+"')>权限</button>　";
						user_list_str += "<button onclick=del_admin_user("+user_list[i].id+")>删除</button>";
					user_list_str += "</div>";
				user_list_str += "</div>";
			}
			$('#user_list_area').html(user_list_str);
			
			Fpage(pnow,max_page,function(p){
				pnow = p;
				sessionStorage.after_con_admin_pnow = p;
				max_page = parseInt(p) + 1;
				get_admin_user_list();
			},max_page,1,'#fpage');
		}else{
			Fpage(pnow,pnow,function(p){
				pnow = p;
				sessionStorage.after_con_admin_pnow;
				if(p == 1){
					max_page = 2;
				}else{
					max_page = p;
				}
				get_admin_user_list();
			},max_page,1,'#fpage');
			$('#user_list_area').html('');
		}
	});
}



//删除用户
function del_admin_user(id){
	Ajax2({type:'del_admin_user',id:id},function(res){
		if(res == 'ok'){
			A('操作成功！');
			
			get_admin_user_list();	//获取用户列表 
		}else{
			A('操作异常！');
		}
	});
}

//更改状态
function change_admin_user_stat(id){
	Ajax2({type:'change_admin_user_stat',id:id},function(res){
		if(res == 'ok'){
			A('操作成功！');
			get_admin_user_list();	//获取用户列表 
		}else{
			A('操作异常！');
		}
	});
}

function jump_perm(id,user){
	if(!id || !user){
		return;
	}
	sessionStorage.current_admin_id = id;
	sessionStorage.currnet_admin_name = user;
	location.replace('after_con_admin_perm.html');
}
</script>